package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.moleq.base.EditableListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.GlobalDiscountSetting;
import com.moleq.util.MoleQUtil;

public class GlobalDiscountAdapter extends MQBaseAdapter {

	public static final String GLOBALDISCOUNT_EDITED = "GlobalDiscountEdit";
	private LayoutInflater mInflater;
	private List<GlobalDiscountSetting> listGlobalDiscount;
	private EditableListener mEditableListener;
	private Context shareContext;

	public GlobalDiscountAdapter(Context context, List<GlobalDiscountSetting> listGlobalDiscount) {
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listGlobalDiscount = listGlobalDiscount;
	}

	public void setEditableListener(EditableListener listener) {
		mEditableListener = listener;
	}

	public void setSource(List<GlobalDiscountSetting> listGlobalDiscount) {
		this.listGlobalDiscount = listGlobalDiscount;
	}

	public int getCount() {
		return listGlobalDiscount.size();
	}

	public Object getItem(int position) {
		return listGlobalDiscount.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);

		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ds_global_discount_list, null);
			holder.et_global_discountno = (EditText) convertView.findViewById(R.id.et_global_discountno);
			holder.et_global_discountdesc = (EditText) convertView.findViewById(R.id.et_global_discountdesc);
			holder.et_global_discountamt = (EditText) convertView.findViewById(R.id.et_global_discountamt);

			holder.et_global_discountdesc.addTextChangedListener(mMyTextWatcher);
			holder.et_global_discountdesc.setOnTouchListener(mOnTouchListener);
			holder.et_global_discountamt.addTextChangedListener(mMyTextWatcher);
			holder.et_global_discountamt.setOnTouchListener(mOnTouchListener);
			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}

		holder.et_global_discountno.setText(String.valueOf(position + 1));
		holder.et_global_discountdesc.setTag(position);
		holder.et_global_discountamt.setTag(position);

		String desc = listGlobalDiscount.get(position).getGd_desc();
		holder.et_global_discountdesc.setText(desc);

		double value = listGlobalDiscount.get(position).getGd_value();
		if (value != 0) {
			holder.et_global_discountamt.setText(String.format("%.2f", value));
		}
		return convertView;
	}

	public final class ViewHolder {
		public EditText et_global_discountno;
		public EditText et_global_discountdesc;
		public EditText et_global_discountamt;
	}

	@Override
	public void onTextChange(int resId, String changedText) {
		switch (resId) {
		case R.id.et_global_discountdesc:
			listGlobalDiscount.get(selectedRow).setGd_desc(changedText);
			break;
		case R.id.et_global_discountamt:
			if (MoleQUtil.isNumeric2(changedText)) {
				listGlobalDiscount.get(selectedRow).setGd_value(Double.valueOf(changedText));
			} else {
				listGlobalDiscount.get(selectedRow).setGd_value(0);
			}
			break;
		}
		listGlobalDiscount.get(selectedRow).setEdit(true);
		mEditableListener.onEdited(GLOBALDISCOUNT_EDITED);

	}
}
